package org.jeecg.modules.demo.ddyht.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.ddyht.entity.*;
import org.jeecg.modules.demo.ddyht.service.IDdygCzrzService;
import org.jeecg.modules.demo.ddyht.service.IDdygInventoryService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.demo.ddyht.service.IDdygPiciService;
import org.jeecg.modules.demo.ddyht.service.IJxcCostingService;
import org.jeecg.modules.demo.jxcx.service.*;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 库存明细
 * @Author: jeecg-boot
 * @Date:   2025-02-23
 * @Version: V1.0
 */
@Api(tags="库存明细")
@RestController
@RequestMapping("/ddyht/jxcInventory")
@Slf4j
public class DdygInventoryController extends JeecgController<DdygInventory, IDdygInventoryService> {
	@Autowired
	private IDdygInventoryService jxcInventoryService;
	@Autowired
	private IDdygPiciService ddygPiciService;
	@Autowired
	private IDdygCzrzService ddygCzrzService;
	@Autowired
	private JxcUserService jxcUserService;
	@Autowired
	private JxcOpenuserService jxcOpenuserService;
	@Autowired
	private JxcSaleService jxcSaleService;
	@Autowired
	private JxcStockService jxcStockService;
	@Autowired
	private JxcCostService jxcCostService;
	@Autowired
	private IJxcCostingService jxcCostingService;

	/**
	 * 分页列表查询
	 *
	 * @param ddygInventory
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "库存明细-分页列表查询")
	@ApiOperation(value="库存明细-分页列表查询", notes="库存明细-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<DdygInventory>> queryPageList(DdygInventory ddygInventory,
													  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													  HttpServletRequest req) {
		QueryWrapper<DdygInventory> queryWrapper = QueryGenerator.initQueryWrapper(ddygInventory, req.getParameterMap());
		Page<DdygInventory> page = new Page<DdygInventory>(pageNo, pageSize);
		IPage<DdygInventory> pageList = jxcInventoryService.page(page, queryWrapper);
		List<DdygInventory> company = jxcInventoryService.getBycompany(ddygInventory.getUsername());
		if (StringUtils.isNotBlank(ddygInventory.getType1())){
			List<DdygInventory> list = new ArrayList<>();
			String type1 = ddygInventory.getType1();
			if (type1.equals("only")){
				if (ddygInventory.getType2().equals("model")){
					if (StringUtils.isBlank(ddygInventory.getType3())){
						int size = company.size();
						int page2 = (size + 9) / 10;
						pageList.setTotal(size);
						pageList.setPages(page2);
						pageList.setRecords(company);
						return Result.OK(pageList);
					}
					String type3 = ddygInventory.getType3();
					for (int i = 0; i < company.size(); i++) {
						if (StringUtils.containsIgnoreCase(company.get(i).getModel(),type3)){
							list.add(company.get(i));
						}
					}
				}
			}
			int size = list.size();
			int page2 = (size + 9) / 10;
			pageList.setTotal(size);
			pageList.setPages(page2);
			pageList.setRecords(list);
			setamount(ddygInventory.getUsername());
			return Result.OK(pageList);
		}
		List<DdygInventory> company2 = new ArrayList<>();
		for (int i = 0; i < company.size(); i++) {
			DdygInventory ddygInventory1 = company.get(i);
			if (!ddygInventory1.getNumber().equals("0")){
				company2.add(ddygInventory1);
			}
		}
		int size = company.size();
		int page2 = (size + 9) / 10;
		pageList.setTotal(size);
		pageList.setPages(page2);
		pageList.setRecords(company2);
		setamount(ddygInventory.getUsername());
		return Result.OK(pageList);
	}
	 @GetMapping(value = "/tz")
	 public Result<String> tz(String username) {
		if (StringUtils.isNotBlank(jxcInventoryService.tz(username))){
			return Result.OK(jxcInventoryService.tz(username));
		}else {
			return Result.error("暂无专属大屏");
		}

	 }
	 @GetMapping(value = "/xz")
	 public Result<String> mb(String id) {
		 String mb = jxcInventoryService.xz();
		 return Result.OK(mb);
	 }
	/**
	 *   添加
	 *
	 * @param ddygInventory
	 * @return
	 */
	@AutoLog(value = "库存明细-添加")
	@ApiOperation(value="库存明细-添加", notes="库存明细-添加")
//	@RequiresPermissions("ddyht:jxc_inventory:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody DdygInventory ddygInventory) {
		jxcInventoryService.save(ddygInventory);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param ddygInventory
	 * @return
	 */
	@AutoLog(value = "库存明细-编辑")
	@ApiOperation(value="库存明细-编辑", notes="库存明细-编辑")
//	@RequiresPermissions("ddyht:jxc_inventory:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody DdygInventory ddygInventory) {
		jxcInventoryService.updateById(ddygInventory);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "库存明细-通过id删除")
	@ApiOperation(value="库存明细-通过id删除", notes="库存明细-通过id删除")
//	@RequiresPermissions("ddyht:jxc_inventory:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		jxcInventoryService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "库存明细-批量删除")
	@ApiOperation(value="库存明细-批量删除", notes="库存明细-批量删除")
//	@RequiresPermissions("ddyht:jxc_inventory:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.jxcInventoryService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "库存明细-通过id查询")
	@ApiOperation(value="库存明细-通过id查询", notes="库存明细-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<DdygInventory> queryById(@RequestParam(name="id",required=true) String id) {
		DdygInventory ddygInventory = jxcInventoryService.getById(id);
		if(ddygInventory ==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(ddygInventory);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param ddygInventory
    */
//    @RequiresPermissions("ddyht:jxc_inventory:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DdygInventory ddygInventory) {
//        return super.exportXls(request, ddygInventory, DdygInventory.class, "库存明细");
		log.info("ddygInventory",ddygInventory);
		log.info("ddygInventorygetUsername",ddygInventory.getUsername());
		return exportXls4(request, ddygInventory, DdygInventory.class, "库存明细");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
//    @RequiresPermissions("ddyht:jxc_inventory:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DdygInventory.class);
    }
	 public ModelAndView exportXls4(HttpServletRequest request, DdygInventory object, Class<DdygInventory> clazz, String title) {
		 // Step.1 组装查询条件
		 QueryWrapper<DdygInventory> queryWrapper = QueryGenerator.initQueryWrapper(object, request.getParameterMap());
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		 // 过滤选中数据
		 String selections = request.getParameter("selections");
		 if (oConvertUtils.isNotEmpty(selections)) {
			 List<String> selectionList = Arrays.asList(selections.split(","));
			 queryWrapper.in("id",selectionList);
		 }
		 // Step.2 获取导出数据
		 List<DdygInventory> exportList = jxcInventoryService.getBycompany(object.getUsername());
		 log.info("object.getUsername()",object.getUsername());
		 log.info("exportList:{}",exportList);

		 // Step.3 AutoPoi 导出Excel
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 //此处设置的filename无效 ,前端会重更新设置一下
		 mv.addObject(NormalExcelConstants.FILE_NAME, title);
		 mv.addObject(NormalExcelConstants.CLASS, clazz);
		 //update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
		 ExportParams exportParams=new ExportParams(title + "报表", "导出人:" + object.getUsername(), title);
//		 ExportParams exportParams=new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title);
		 exportParams.setImageBasePath("C:/upload/sc");
//		 exportParams.setImageBasePath(jeecgBaseConfig.getPath().getUpload());
		 //update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
		 mv.addObject(NormalExcelConstants.PARAMS,exportParams);
		 mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		 return mv;
	 }
	 @RequestMapping("/getamount")
	 @ResponseBody
	 public Result<String> getamount(@RequestBody JSONObject json) throws Exception {
		 String day = json.getString("date");
		 String companyId = json.getString("companyId");
		 log.info("获取今日出货量进货量利润请求：" + json.toString());
		 JSONObject result = new JSONObject();
		 JSONObject data = new JSONObject();
		 if (jxcStockService.getamount(day,companyId) == null){
			 data.put("Stockamount", "无数据");
			 log.info(day+"进货金额无数据");
		 }else {
			 BigDecimal bigDecimal = new BigDecimal(jxcStockService.getamount(day, companyId));
			 data.put("Stockamount",bigDecimal );
			 log.info(day+"进货金额：" + bigDecimal);
		 }
		 if (jxcSaleService.getamount(day,companyId) == null){
			 data.put("Saleamount", "无数据");
			 log.info(day+"销售金额无数据");
		 }else {
			 BigDecimal bigDecimal = new BigDecimal(jxcSaleService.getamount(day, companyId));
			 data.put("Saleamount", bigDecimal);
			 log.info(day+"销售金额：" + bigDecimal);
		 }
		 BigDecimal Costallamount = new BigDecimal(0);
		 if (jxcCostService.getamount(day, companyId) != null){
			 Costallamount = new BigDecimal(jxcCostService.getamount(day, companyId));
		 }
		 BigDecimal Costingallamount = new BigDecimal(0);
		 if (jxcCostingService.getamount(day, companyId) != null){
			 Costingallamount = new BigDecimal(jxcCostingService.getamount(day, companyId));
		 }
//		 BigDecimal Costingallamount = new BigDecimal(jxcCostingService.getamount(day, companyId));
		 log.info(Costingallamount+"Costingallamount");
		 BigDecimal Costamount;
		 if (!Costallamount.equals(BigDecimal.ZERO) || !Costingallamount.equals(BigDecimal.ZERO)) {
			 if (!Costingallamount.equals(BigDecimal.ZERO)) {
				 if (!Costallamount.equals(BigDecimal.ZERO)){
					 Costamount = Costingallamount.add(Costallamount);
				 }else {
					 Costamount = Costingallamount;
				 }
			 }else if (!Costallamount.equals(BigDecimal.ZERO)) {
				 Costamount = Costallamount;
			 }else {
				 Costamount = BigDecimal.ZERO;
			 }
			 log.info(day+"成本金额：" + Costamount);
			 log.info("------------");
			 data.put("Costamount", Costamount);
			 if (data.getString("Stockamount").equals("无数据")){
				 if (data.getString("Saleamount").equals("无数据")){
					 //无销售无进货有成本
					 data.put("profit","-"+Costamount);
				 }else {
					 //有销售无进货有成本
					 data.put("profit",new BigDecimal(data.getString("Saleamount")).subtract(Costamount));
				 }
			 }else {
				 BigDecimal bigDecimal = new BigDecimal(data.getString("Stockamount")).add(Costamount);
				 log.info("有进货有成本");
				 log.info("进货和成本总和"+bigDecimal);
				 if (data.getString("Saleamount").equals("无数据")){
					 //无销售有进货有成本
					 data.put("profit","-"+Costamount);
				 }else {
					 //有销售有进货有成本
					 data.put("profit",new BigDecimal(data.getString("Saleamount")).subtract(Costamount));
					 log.info("有销售有进货有成本");
				 }
			 }
		 }else {
			 if (data.getString("Stockamount").equals("无数据")){
				 if (data.getString("Saleamount").equals("无数据")){
					 //无销售无进货无成本
					 data.put("profit","0");
				 }else {
					 //有销售无进货无成本
					 data.put("profit",data.getString("Saleamount"));
				 }
			 }else {
				 if (data.getString("Saleamount").equals("无数据")){
					 //无销售有进货无成本
					 data.put("profit","0");
//					 data.put("profit","-"+data.getString("Stockamount"));
				 }else {
					 //有销售有进货无成本
					 data.put("profit",new BigDecimal(data.getString("Saleamount")));
				 }
			 }

			 data.put("Costamount", "无数据");
			 log.info(day+"成本金额无数据");
		 }
		 result.put("data", data);
		 log.info("result.toString()--------------");
		 log.info(result.toString());
		 JxcDapingshuju selectshuju = jxcInventoryService.selectshuju(companyId);
		 if (selectshuju != null){
			 jxcInventoryService.updateshuju(data.getString("Saleamount"), data.getString("Stockamount"), data.getString("Costamount"), data.getString("profit"),companyId);
		 }else {
			 JxcDapingshuju jxcDapingshuju = new JxcDapingshuju();
			 jxcDapingshuju.setCompanyId(companyId);
			 jxcDapingshuju.setRiben(data.getString("Costamount"));
			 jxcDapingshuju.setRijin(data.getString("Stockamount"));
			 jxcDapingshuju.setRisell(data.getString("Saleamount"));
			 jxcDapingshuju.setRirun(data.getString("profit"));
			 jxcInventoryService.saveri(jxcDapingshuju);
			 JSONObject jsonObject1 = new JSONObject();
			 jsonObject1.put("date", day.substring(0, 6));
			 log.info(day.substring(0, 6));
			 log.info(day.substring(0, 4));
			 jsonObject1.put("companyId", companyId);
			 getyueamount(jsonObject1);
			 JSONObject jsonObject2 = new JSONObject();
			 jsonObject2.put("date", day.substring(0, 4));
			 jsonObject2.put("companyId", companyId);
			 getnianamount(jsonObject2);
		 }
		 return Result.ok("");
	 }
	 @RequestMapping("/getyueamount")
	 @ResponseBody
	 public Result<String> getyueamount(@RequestBody JSONObject json) throws Exception {
		 String day = json.getString("date");
		 String companyId = json.getString("companyId");
		 log.info("获取今月出货量进货量利润请求：" + json.toString());
		 JSONObject result = new JSONObject();
		 JSONObject data = new JSONObject();
		 if (jxcStockService.getyueamount(day,companyId) == null){
			 data.put("Stockamount", "无数据");
			 log.info(day+"进货金额无数据");
		 }else {
			 BigDecimal bigDecimal = new BigDecimal(jxcStockService.getyueamount(day, companyId));
			 data.put("Stockamount",bigDecimal );
			 log.info(day+"进货金额：" + bigDecimal);
		 }
		 if (jxcSaleService.getyueamount(day,companyId) == null){
			 data.put("Saleamount", "无数据");
			 log.info(day+"销售金额无数据");
		 }else {
			 BigDecimal bigDecimal = new BigDecimal(jxcSaleService.getyueamount(day, companyId));
			 data.put("Saleamount", bigDecimal);
			 log.info(day+"销售金额：" + bigDecimal);
		 }
		 BigDecimal Costallamount = new BigDecimal(0);
		 if (jxcCostService.getyueamount(day, companyId) != null){
			 Costallamount = new BigDecimal(jxcCostService.getyueamount(day, companyId));
		 }
		 log.info(Costallamount+"Costallamount");
		 BigDecimal Costingallamount = new BigDecimal(0);
		 if (jxcCostingService.getyueamount(day, companyId) != null){
			 Costingallamount = new BigDecimal(jxcCostingService.getyueamount(day, companyId));
		 }
//		 BigDecimal Costingallamount = new BigDecimal(jxcCostingService.getyueamount(day, companyId));
		 log.info(Costingallamount+"Costingallamount");
		 BigDecimal Costamount;
		 if (!Costallamount.equals(BigDecimal.ZERO) || !Costingallamount.equals(BigDecimal.ZERO)) {
			 if (!Costingallamount.equals(BigDecimal.ZERO)) {
				 if (!Costallamount.equals(BigDecimal.ZERO)){
					 Costamount = Costingallamount.add(Costallamount);
				 }else {
					 Costamount = Costingallamount;
				 }
			 }else if (!Costallamount.equals(BigDecimal.ZERO)) {
				 Costamount = Costallamount;
			 }else {
				 Costamount = BigDecimal.ZERO;
			 }
			 log.info(day+"成本金额：" + Costamount);
			 log.info("------------");
			 data.put("Costamount", Costamount);
			 if (data.getString("Stockamount").equals("无数据")){
				 if (data.getString("Saleamount").equals("无数据")){
					 //无销售无进货有成本
					 data.put("profit","-"+Costamount);
				 }else {
					 //有销售无进货有成本
					 data.put("profit",new BigDecimal(data.getString("Saleamount")).subtract(Costamount));
				 }
			 }else {
				 BigDecimal bigDecimal = new BigDecimal(data.getString("Stockamount")).add(Costamount);
				 log.info("有进货有成本");
				 log.info("进货和成本总和"+bigDecimal);
				 if (data.getString("Saleamount").equals("无数据")){
					 //无销售有进货有成本
					 data.put("profit","-"+Costamount);
				 }else {
					 //有销售有进货有成本
					 data.put("profit",new BigDecimal(data.getString("Saleamount")).subtract(Costamount));
					 log.info("有销售有进货有成本");
				 }
			 }
		 }else {
			 if (data.getString("Stockamount").equals("无数据")){
				 if (data.getString("Saleamount").equals("无数据")){
					 //无销售无进货无成本
					 data.put("profit","0");
				 }else {
					 //有销售无进货无成本
					 data.put("profit",data.getString("Saleamount"));
				 }
			 }else {
				 if (data.getString("Saleamount").equals("无数据")){
					 //无销售有进货无成本
					 data.put("profit","0");
//					 data.put("profit","-"+data.getString("Stockamount"));
				 }else {
					 //有销售有进货无成本
					 data.put("profit",new BigDecimal(data.getString("Saleamount")));
				 }
			 }

			 data.put("Costamount", "无数据");
			 log.info(day+"成本金额无数据");
		 }
		 result.put("data", data);
		 log.info("result.toString()--------------");
		 log.info(result.toString());
		 JxcDapingshuju selectshuju = jxcInventoryService.selectshuju(companyId);
		 if (selectshuju != null){
			 jxcInventoryService.updateshujuyue(data.getString("Saleamount"), data.getString("Stockamount"), data.getString("Costamount"), data.getString("profit"),companyId);
		 }
//		 else {
//			 JxcDapingshuju jxcDapingshuju = new JxcDapingshuju();
//			 jxcDapingshuju.setCompanyId(companyId);
//			 jxcDapingshuju.setYueben(data.getString("Costamount"));
//			 jxcDapingshuju.setYuejin(data.getString("Stockamount"));
//			 jxcDapingshuju.setYuesell(data.getString("Saleamount"));
//			 jxcDapingshuju.setYuerun(data.getString("profit"));
//			 jxcInventoryService.saveyue(jxcDapingshuju);
//		 }
		 return Result.ok("");
	 }
	 /**
	  * 获取本年出货量进货量利润
	  *
	  * @throws IOException
	  * @throws
	  */
	 @RequestMapping("/getnianamount")
	 @ResponseBody
	 public Result<String> getnianamount(@RequestBody JSONObject json) throws Exception {
		 String day = json.getString("date");
		 String companyId = json.getString("companyId");
		 log.info("获取今年出货量进货量利润请求：" + json.toString());
		 JSONObject result = new JSONObject();
		 JSONObject data = new JSONObject();
		 if (jxcStockService.getnianamount(day,companyId) == null){
			 data.put("Stockamount", "无数据");
			 log.info(day+"进货金额无数据");
		 }else {
			 BigDecimal bigDecimal = new BigDecimal(jxcStockService.getnianamount(day, companyId));
			 data.put("Stockamount",bigDecimal );
			 log.info(day+"进货金额：" + bigDecimal);
		 }
		 if (jxcSaleService.getnianamount(day,companyId) == null){
			 data.put("Saleamount", "无数据");
			 log.info(day+"销售金额无数据");
		 }else {
			 BigDecimal bigDecimal = new BigDecimal(jxcSaleService.getnianamount(day, companyId));
			 data.put("Saleamount", bigDecimal);
			 log.info(day+"销售金额：" + bigDecimal);
		 }
		 BigDecimal Costallamount = new BigDecimal(0);
		 if (jxcCostService.getnianamount(day, companyId) != null){
			 Costallamount = new BigDecimal(jxcCostService.getnianamount(day, companyId));
		 }
		 log.info(Costallamount+"Costallamount");
		 BigDecimal Costingallamount = new BigDecimal(0);
		 if (jxcCostingService.getnianamount(day, companyId) != null){
			 Costingallamount = new BigDecimal(jxcCostingService.getnianamount(day, companyId));
		 }
//		 BigDecimal Costingallamount = new BigDecimal(jxcCostingService.getnianamount(day, companyId));
		 log.info(Costingallamount+"Costingallamount");
		 BigDecimal Costamount;
		 if (!Costallamount.equals(BigDecimal.ZERO) || !Costingallamount.equals(BigDecimal.ZERO)) {
			 if (!Costingallamount.equals(BigDecimal.ZERO)) {
				 if (!Costallamount.equals(BigDecimal.ZERO)){
					 Costamount = Costingallamount.add(Costallamount);
				 }else {
					 Costamount = Costingallamount;
				 }
			 }else if (!Costallamount.equals(BigDecimal.ZERO)) {
				 Costamount = Costallamount;
			 }else {
				 Costamount = BigDecimal.ZERO;
			 }
			 log.info(day+"成本金额：" + Costamount);
			 log.info("------------");
			 data.put("Costamount", Costamount);
			 if (data.getString("Stockamount").equals("无数据")){
				 if (data.getString("Saleamount").equals("无数据")){
					 //无销售无进货有成本
					 data.put("profit","-"+Costamount);
				 }else {
					 //有销售无进货有成本
					 data.put("profit",new BigDecimal(data.getString("Saleamount")).subtract(Costamount));
				 }
			 }else {
				 BigDecimal bigDecimal = new BigDecimal(data.getString("Stockamount")).add(Costamount);
				 log.info("有进货有成本");
				 log.info("进货和成本总和"+bigDecimal);
				 if (data.getString("Saleamount").equals("无数据")){
					 //无销售有进货有成本
					 data.put("profit","-"+Costamount);
				 }else {
					 //有销售有进货有成本
					 data.put("profit",new BigDecimal(data.getString("Saleamount")).subtract(Costamount));
					 log.info("有销售有进货有成本");
				 }
			 }
		 }else {
			 if (data.getString("Stockamount").equals("无数据")){
				 if (data.getString("Saleamount").equals("无数据")){
					 //无销售无进货无成本
					 data.put("profit","无数据");
				 }else {
					 //有销售无进货无成本
					 data.put("profit",data.getString("Saleamount"));
				 }
			 }else {
				 if (data.getString("Saleamount").equals("无数据")){
					 //无销售有进货无成本
//					 data.put("profit","-"+data.getString("Stockamount"));
					 data.put("profit","0");
				 }else {
					 //有销售有进货无成本
					 data.put("profit",new BigDecimal(data.getString("Saleamount")));
				 }
			 }

			 data.put("Costamount", "无数据");
			 log.info(day+"成本金额无数据");
		 }
		 result.put("data", data);
		 log.info("result.toString()--------------");
		 log.info(result.toString());

		 JxcDapingshuju selectshuju = jxcInventoryService.selectshuju(companyId);
		 if (selectshuju != null){
			 jxcInventoryService.updateshujunian(data.getString("Saleamount"), data.getString("Stockamount"), data.getString("Costamount"), data.getString("profit"),companyId);
		 }
//		 else {
//			 log.info("22222");
//			 JxcDapingshuju jxcDapingshuju = new JxcDapingshuju();
//			 jxcDapingshuju.setCompanyId(companyId);
//			 jxcDapingshuju.setNianben(data.getString("Costamount"));
//			 jxcDapingshuju.setNianjin(data.getString("Stockamount"));
//			 jxcDapingshuju.setNiansell(data.getString("Saleamount"));
//			 jxcDapingshuju.setNianrun(data.getString("profit"));
//			 jxcInventoryService.savenian(jxcDapingshuju);
//		 }
		 return Result.ok("");
	 }

	 @RequestMapping("/deleteall")
	 @ResponseBody
	 public Result<String> deleteall(@RequestBody JSONObject json){
		 String companyId = json.getString("companyId");
		 ddygCzrzService.deleteallInventory(companyId);
		 ddygCzrzService.deleteallPici(companyId);
		 ddygCzrzService.deleteallCost(companyId);
		 ddygCzrzService.deleteallSale(companyId);
		 ddygCzrzService.deleteallStock(companyId);
		 ddygCzrzService.deleteallCustom(companyId);
		 ddygCzrzService.deleteallSupplier(companyId);
		 ddygCzrzService.deleteallPayinfor(companyId);
		 ddygCzrzService.deleteallCosting(companyId);
		 ddygCzrzService.deleteallCostingPici(companyId);
		 ddygCzrzService.deleteallHbzj(companyId);
		 ddygCzrzService.deleteallCzrz(companyId);
		 return Result.ok("删除成功,数据已清空");

	 }
	 public void setamount(String companyId){
		 List<DdygInventory> bycompany = jxcInventoryService.getBycompany(companyId);
		 for (int i = 0; i < bycompany.size(); i++) {
			 DdygInventory ddygInventory = bycompany.get(i);
			 String model = ddygInventory.getModel();
			 List<DdygPici> byall = ddygPiciService.getByall(companyId, "1", model);
			 if (byall.size() > 0){
				 BigDecimal allamount = new BigDecimal(0);
				 BigDecimal allNumber = new BigDecimal(0);
				 for (int j = 0; j < byall.size(); j++) {
					 DdygPici ddygPici = byall.get(j);
					 allamount = allamount.add(new BigDecimal(ddygPici.getAmount()));
					 allNumber = allNumber.add(new BigDecimal(ddygPici.getNumber()));
				 }
				 if (allNumber != BigDecimal.ZERO){
					 BigDecimal avgunitPrice = allamount.divide(allNumber, 2, BigDecimal.ROUND_HALF_UP);
					 ddygInventory.setUnitPrice(avgunitPrice.toString());
					 ddygInventory.setAmount(avgunitPrice.multiply(new BigDecimal(ddygInventory.getNumber())).toString());
				 }else {
					 ddygInventory.setUnitPrice("0");
					 ddygInventory.setAmount("0");
				 }
				 jxcInventoryService.updateById(ddygInventory);

			 }else {
				 DdygInventory bymodelcompany = jxcInventoryService.getBymodelcompany(model, companyId);
				 bymodelcompany.setAmount("0");
				 jxcInventoryService.updateById(bymodelcompany);
			 }

		 }
	 }
}
